package ar.com.ford.it.fullEconomics.view.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;

public class WslActionRedirect extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String wslUrl = "https://www.wsl.ford.com/login.cgi";

        /* 
         * generar la url de retorno a partir de la info que viene en el request
         * "http://ee.ford.com:11000/fullEconomics/Begin.do"
         */
        String urlBack = request.getRequestURL().toString();
        System.out.println("url::" + urlBack);
        
        int ini = urlBack.length() - 8;
        
        urlBack = urlBack.substring(0, ini-1); 
        System.out.println("el nuevo url es::"+urlBack);

        if (wslUrl == null || urlBack == null) {
            // return mapping.findForward("error_application");
            return null;
        }

        if ("".equals(wslUrl) || "".equals(urlBack)) {
            // return mapping.findForward("error_application");
            return null;
        }

        ActionRedirect redirect = new ActionRedirect();

        redirect.setPath(wslUrl);

        redirect.addParameter("back", urlBack);
        
        return redirect;
    }

}